/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.createdtmassmethodgen;

import com.inspur.edp.bef.bizentity.beenum.BETriggerTimePointType;
import com.inspur.edp.bef.bizentity.common.OperationConvertUtils;
import com.inspur.edp.bef.bizentity.operation.Validation;
import com.inspur.edp.bef.core.determination.builtinimpls.predicate.ExecutePredicateFactory;
import com.inspur.edp.bef.core.validation.builtinimpls.BefAfterSaveValAssembler;
import com.inspur.edp.bef.core.validation.builtinimpls.adaptors.BefAftSaveValAdaptor;
import com.inspur.edp.bef.core.validation.builtinimpls.adaptors.BefPredicateValAdaptor;
import com.inspur.edp.bef.dtgenerator.common.Utils;
import com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.entityimpl.BefEntityImplGenCtx;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.designtime.api.operation.CommonDetermination;
import com.inspur.edp.cef.designtime.api.operation.CommonValidation;
import com.inspur.edp.cef.entity.changeset.ChildElementsTuple;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.createvalassmethod.EntityCreateValAssMethodGenerator;
import com.inspur.edp.cef.generator.overall.GenUtils;
import java.util.List;

// public IEntityRTValidationAssembler createAfterSaveValAssembler() {
//     return null;
// }
public class CreateAfterSaveValAssMethodGenerator extends EntityCreateValAssMethodGenerator {

  private final BefEntityImplGenCtx entityCtx;

  public CreateAfterSaveValAssMethodGenerator(BefEntityImplGenCtx entityCtx, String nodeCode) {
    super(entityCtx, nodeCode);
    this.entityCtx = entityCtx;
  }

  @Override
  protected List<CommonValidation> getValMediateTypes() {
    return entityCtx.getDataType().getValAfterSave();
  }

  @Override
  protected TypeInfo getValAssClassType() {
    addImport(BefAfterSaveValAssembler.class.getTypeName());
    return new TypeInfo(BefAfterSaveValAssembler.class);
  }

  @Override
  protected TypeInfo getNewAdaptor_TypeParam(CommonValidation op) {
    return CreateValAssMethodUtil.anyChildRequestElement(op)
        ? new TypeInfo(BefAftSaveValAdaptor.class)
        : new TypeInfo(BefPredicateValAdaptor.class);
  }

  @Override
  protected String getMethodName() {
    return "addAfterSaveVal";
  }

  @Override
  protected void buildExtendNewAdaptorParameter(CommonValidation val, List args) {
    if(CreateValAssMethodUtil.anyChildRequestElement(val)) {
      Validation v = (Validation) entityCtx.getDataType().getValidations().getItem(val.getID());
      args.add(Utils
          .buildChgTypes(ast, val.getGetExecutingDataStatus()));
      if (v.getRequestElements().isEmpty() && v.getRequestChildElements().isEmpty()) {
        return;
      }
      args.add(GenUtils.buildRequestElementsList(ast, getEntityImplGenContext().getDataType(),
          v.getRequestElements()));
      args.add(
          Utils.buildRequestChildElements(ast, entityCtx.getDataType(), v.getRequestChildElements()));
    } else {
      addImport(ExecutePredicateFactory.class.getTypeName());
      args.add(CreateValAssMethodUtil.buildPredicate(ast, val, getEntityImplGenContext()));
    }
//    Validation v = (Validation) entityCtx.getDataType().getValidations().getItem(val.getID());
//    args.add(Utils
//        .buildChgTypes(ast, v.getValidationTriggerPoints().get(BETriggerTimePointType.AfterSave)));
//    if (v.getRequestElements().isEmpty() && v.getRequestChildElements().isEmpty()) {
//      return;
//    }
//    args.add(GenUtils.buildRequestElementsList(ast, getEntityImplGenContext().getDataType(),
//        v.getRequestElements()));
//    args.add(
//        Utils.buildRequestChildElements(ast, entityCtx.getDataType(), v.getRequestChildElements()));
  }
}
